<?php 
class CloudControlService
{
	protected $m_CommandQueueDO  = "appcloudcontrol.command.do.CommandQueueSystemDO";
	protected $m_ServerDO		 = "appcloudcontrol.server.do.ServerSystemDO";
	protected $m_InstanceDO 	 = "appcloudcontrol.instance.do.InstanceSystemDO";
	
	public function addCommand($serverId,$cmd,$param=null,$callback=null,$callback_param=null)
	{		
		
		$record = array(
			"server_id" => $serverId,
			"command" => $cmd,
			"parameter" => serialize($param),
			"callback" => $callback,
			"callback_parameter" => serialize($callback_param),
			"status" => 'pending',
		
		);
		BizSystem::getObject($this->m_CommandQueueDO)->insertRecord($record);
		
		if(strtolower($cmd)=='instance::create')
		{
			$this->_reserveHostname($param['hostname'], $param['domain'],$param['foreign_key']);
		}
		
		return true;
	}	
	
	protected function _reserveHostname($hostname,$domain,$foreign_key)
	{
		$record = array(
			"hostname" 		=> $hostname,
			"domain"	 	=> $domain,
			"foreign_key" 	=> $foreign_key
		);
		return BizSystem::getObject($this->m_InstanceDO)->insertRecord($record);
	}
	
	public function fetchServerInfo($id)
	{
		$serverRec = BizSystem::getObject($this->m_ServerDO)->fetchById($id);
		return $serverRec;
	}
	
	public function checkHostnameExist($hostname,$domain)
	{
		$searchRule = "[hostname]='$hostname' AND [domain]='$domain'";
		$hostRec = BizSystem::getObject($this->m_InstanceDO)->fetchOne($searchRule);
		if($hostRec)
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	public function fetchInstanceInfoByFK($foreign_key)
	{
		$searchRule = "[foreign_key]='$foreign_key'";
		$instanceRec = BizSystem::getObject($this->m_InstanceDO)->fetchOne($searchRule);
		return $instanceRec;
	}
	
	public function fetchInstanceInfoByFQDN($fqdn)
	{
		$searchRule = "[fqdn]='$fqdn'";
		$instanceRec = BizSystem::getObject($this->m_InstanceDO)->fetchOne($searchRule);
		return $instanceRec;
	}
}
?>